import { BaseWindow } from "../../../component/window/BaseWindow";
import { Language } from "../../../../extend/language/Language";
import { NetDispatcher } from "../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../mvc/ServerCommand";
import { ObjCreate } from "../common/ObjCreate";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../resource/ImagesConst";
import { TilePanel } from "../equipment/TilePanel";
import { BlessPropCellrenderer } from "./BlessPropCellrenderer";
import { ListEvent } from "../../../../fl/events/ListEvent";
import { Cache } from "../../cache/Cache";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { ItemData } from "../../resource/info/ItemData";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { GButton } from "../../../../com/mui/controls/GButton";
import { MsgManager } from "../../manager/MsgManager";
import { Alert } from "../../../../com/mui/controls/Alert";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
type int = number;
//class PetBlessPropWindow
    
    export  class PetBlessPropWindow extends BaseWindow
    {
        constructor()
        {
            super();
            this.setSize(340, 290);
            this.titleHeight = 30;
            this.title = Language.getString(54185);
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.onPackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onPackChangeHandler);
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc1=null;
            super.createChildren();
            ObjCreate.createBg(20, 45, 300, 175, this);
            ObjCreate.createTextField(Language.getString(54186), 22, 225, 190, 20, this, GlobalStyle.textFormat6);
            var loc2=0;
            while (loc2 < 28) 
            {
                loc1 = GlobalClass.getBitmap(ImagesConst.PackItemBg);
                loc1.width = 38;
                loc1.height = 38;
                this.addChild(loc1);
                loc1.x = 28 + loc2 % 7 * 41;
                loc1.y = 51 + Math.floor(loc2 / 7) * 41;
                ++loc2;
            }
            this._propList = new TilePanel();
            this._propList.horizontalGap = 3;
            this._propList.verticalGap = 3;
            this._propList.setSize(285, 165);
            this._propList.columnWidth = 38;
            this._propList.rowHeight = 38;
            this._propList.setStyle("cellRenderer", BlessPropCellrenderer);
            this._propList.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, this.onUseHandler);
            this._propList.move(30, 53);
            this.addChild(this._propList);
            this._useBtn = ObjCreate.createGButton(Language.getString(54187), 85, 250, 70, 24, this);
            this._useBtn.name = "use";
            this._batchUseBtn = ObjCreate.createGButton(Language.getString(54188), 185, 250, 70, 24, this);
            this._useBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClickHandler);
            this._batchUseBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClickHandler);
            return;
        }

        public updateAddInfo(arg1: Boolean, arg2: string): void
        {
            this._isGrowth = arg1;
            this._uid = arg2;
            return;
        }

        /* internal  */updatePropsList(): void
        {
            var loc1=Cache.instance.pack.backPackCache.getItemsByJudge(this.isAddPetBlessItem);
            if (loc1.length > 28) 
            {
                loc1 = loc1.slice(0, 28);
            }
            if (loc1.length > 0) 
            {
                this._propList.dataProvider = new DataProvider(loc1);
            }
            else 
            {
                this._propList.dataProvider = new DataProvider();
                this._propList.selectedIndex = -1;
            }
            return;
        }

        /* internal  */isAddPetBlessItem(arg1: ItemData): Boolean
        {
            if (this._isGrowth) 
            {
                return ItemsUtil.isAddPetBlessItem(arg1) || ItemsUtil.isPetGrowUpMaterial(arg1);
            }
            return ItemsUtil.isAddPetBlessItem(arg1) || ItemsUtil.isPetTalentAdvanceMaterial(arg1);
        }

        /* internal  */onPackChangeHandler(arg1: Object=null): void
        {
            this.updatePropsList();
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(arg1, arg2);
            this.updatePropsList();
            return;
        }

        /* internal  */onUseHandler(arg1: ListEvent): void
        {
            var loc1=arg1.item as ItemData;
            this.useByItemDataArray([loc1], false);
            return;
        }

        /* internal  */onBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc3=false;
            var loc4=null;
            var loc1=(arg1.target as GButton).name;
            var loc2=[];
            if (loc1 != "use") 
            {
                loc2 = this._propList.dataProvider.toArray();
                if (loc2.length == 0) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(54190));
                    return;
                }
                loc3 = true;
            }
            else 
            {
                if (this._propList.selectedIndex == -1) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(54189));
                    return;
                }
                loc4 = this._propList.selectedItem as ItemData;
                loc2.push(loc4);
                loc3 = false;
            }
            this.useByItemDataArray(loc2, loc3);
            return;
        }

        /* internal  */useByItemDataArray(arg1: Array<any>, arg2: Boolean): void
        {
            var itemDataArray: Array<any>;
            var isBatch: Boolean;
            var materials: Array<any>;
            var alertContent: string;
            var dict: Map<any, any> /* flash.utils.Dictionary */;
            var type: string;
            var onBtnClickHandler: Function;

            var loc1;
            materials = null;
            onBtnClickHandler = null;
            itemDataArray = arg1;
            isBatch = arg2;
            onBtnClickHandler = (arg1: int): void
            {
                if (arg1 == Alert.OK) 
                {
                    materials = getUids(itemDataArray);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetAddBless, {"uid": _uid, "materials": materials, "isGrowth": _isGrowth}));
                    Alert.resetToDefault();
                }
                return;
            }
            materials = [];
            alertContent = "";
            dict = this.getInfoDict(itemDataArray);
            type = this._isGrowth ? Language.getString(54191) : Language.getString(54192);
            if (isBatch != false) 
            {
                alertContent = Language.getStringByParam(54552, type, dict["bless"]);
            }
            else 
            {
                alertContent = Language.getStringByParam(54193, dict["amount"], ItemsUtil.getItemName(itemDataArray[0] as ItemData), type, dict["bless"]);
            }
            Alert.show(alertContent, null, Alert.OK | Alert.CANCEL, null, onBtnClickHandler);
            return;
        }

        /* internal  */getInfoDict(arg1: Array<any>): Map<any, any> /* flash.utils.Dictionary */
        {
            var loc2=0;
            var loc3=0;
            var loc4=null;
            var loc1=new Map<any, any> /* flash.utils.Dictionary */();
            var loc5=0;
            var loc6=arg1;
            for(loc4 of loc6) 
            {
                loc2 = loc2 + loc4.data.itemAmount;
                loc3 = loc3 + loc4.effect * loc4.data.itemAmount;
            }
            loc1["amount"] = loc2;
            loc1["bless"] = loc3;
            return loc1;
        }

        /* internal  */getUids(arg1: Array<any>): Array<any>
        {
            var loc2=null;
            var loc1=[];
            var loc3=0;
            var loc4=arg1;
            for(loc2 of loc4) 
            {
                loc1.push(loc2.uid);
            }
            return loc1;
        }

        /* internal */ /* var */_propList: TilePanel;

        /* internal */ /* var */_useBtn: GButton;

        /* internal */ /* var */_batchUseBtn: GButton;

        /* internal */ /* var */_isGrowth: Boolean;

        /* internal */ /* var */_uid: string;
    }
